package com.kuhyygy.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.kuhyygy.R;

public class LocalBroadCastActivity extends Activity {
	
	private IntentFilter intentFilter;
	private SimpleBroadcast simpleReceiver;
	private LocalBroadcastManager lbManager;

	@Override       
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_localbroad_cast);
		lbManager = LocalBroadcastManager.getInstance(this); 
		intentFilter = new IntentFilter();
		intentFilter.addAction("com.kuhyygy.broadcast.LOCAL_BROADCAST");
		simpleReceiver = new SimpleBroadcast();
		lbManager.registerReceiver(simpleReceiver, intentFilter);
		
		Button btn = (Button) this.findViewById(R.id.sendLocalBc);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent("com.kuhyygy.broadcast.LOCAL_BROADCAST");
				lbManager.sendBroadcast(i);
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		lbManager.unregisterReceiver(simpleReceiver);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.broad_cast, menu);
		return true;
	}
	
	class SimpleBroadcast extends BroadcastReceiver {
		
		@Override
		public void onReceive(Context context, Intent i) {
			Toast.makeText(context, "helloworld", Toast.LENGTH_SHORT).show();
		}

	}

}
